import isEmpty from 'lodash/isEmpty'
export default function strHandle({ vm, action, done }) {
  console.log('----字符串操作----', action.extension.strHandleFuc)
  console.log(vm)
  if (action.extension.strHandleFuc.value.ChangeSwitch) {
    // 开启字符串操作时
    const filterFalse = (arr) => arr.filter(Boolean)
    let str = vm.formDataModel //输入框值
    let intercept = filterFalse(action.extension.strHandleFuc.intercept) //字符串截取参数
    let division = filterFalse(action.extension.strHandleFuc.division) //字符串分割参数
    let replace = action.extension.strHandleFuc.replace //字符串替换参数
    if (intercept.length == 2) {
      // 字符串截取
      let start = Number(intercept[0])
      let end = Number(intercept[1])

      if (vm.formDataModel.length <= start) {
        // 输入得截取得第一个数字比长度还大---不截取
        str = vm.formDataModel
      } else if (vm.formDataModel.length > start) {
        str = vm.formDataModel.substring(start, end + 1)
      }
      vm.formDataModel = str
    }

    if (division.length == 2) {
      // 字符串分割

      let divStr = str.split(division[0])
      if (divStr.length >= Number(division[1])) {
        str = str.split(division[0])[1]
      } else {
        str = vm.formDataModel
      }
      vm.formDataModel = str
    }
    if (!isEmpty(replace[0])) {
      // 字符串替换
      let chace = isEmpty(replace[1]) ? '' : replace[1]
      vm.formDataModel = str.replace(replace[0], chace)
    }
  }
  done(null, {})
}
